﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;
//using LinkCard.AdminCP.Common;
using System.Data;
using LinkCard.Utils;

namespace LinkCard.AdminCP.Supplier
{
    public partial class supplier_new : System.Web.UI.Page
    {
        protected Int64 nSupplierID;
       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null)
            {
                Response.Redirect("/login.aspx");
            }

            if (!IsPostBack)
            {
                DataTable dt;
                // Category Option
                dt = DataHelper.SelectTable("SELECT SupplierCateID, SupplierCateName FROM SupplierCategory ORDER BY SupplierCateOrder", false);
                CategoryID.DataSource = dt;
                CategoryID.DataTextField = "SupplierCateName";
                CategoryID.DataValueField = "SupplierCateID";
                CategoryID.DataBind();

                //ParentID
                dt = DataHelper.SelectTable("SELECT [Name] as SupplierName, SupplierID FROM Supplier where SupplierTransparency = 1 ORDER BY [Name]", false);

                DataRow dr = dt.NewRow();
                dr["SupplierName"] = "Select";
                dr["SupplierID"] = 0;

                dt.Rows.InsertAt(dr, 0);

                SupplierParentID.DataSource = dt;
                SupplierParentID.DataTextField = "SupplierName";
                SupplierParentID.DataValueField = "SupplierID";
                SupplierParentID.DataBind();

               
                SupplierParentID.SelectedIndex = 0;
            }

        }


        protected void btnPublish_Click(object sender, EventArgs e)
        {
            addSupplier(2);
            //addSupplierTemp(); - Không duyệt nữa, up lên trực tiếp
            //Lưu log
            var logAction = LogActions.LogItem(getUserID(), nSupplierID, 0, 0, 0, "Thêm mới Đối tác", "PublishSupplier", "AdminCP - Supplier");

            Response.Redirect("/message_box.aspx?why=1&msg=2");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            addSupplier(0);

            var logAction = LogActions.LogItem(getUserID(), nSupplierID, 0, 0, 0, "Lưu tạm Đối tác", "UpdateSupplierTemp", "AdminCP - Supplier");

            Response.Redirect("/message_box.aspx?why=1&msg=2");
        }

        protected void addSupplier(byte status) {
            var dao = new SupplierDao();

            var _supplier = new LinkCard.Model.Supplier();
            
            _supplier.Name = Name.Text;
            _supplier.Director = Director.Text;
            _supplier.Address = Address.Text;
            _supplier.Phone = Phone.Text;
            _supplier.Fax = Fax.Text;
            _supplier.Email = Email.Text;
            _supplier.TaxCode = TaxCode.Text;
            _supplier.ContactPersonName = ContactPersonName.Text;
            _supplier.ContactPersonMail = ContactPersonMail.Text;
            _supplier.Business = Business.Text;
            _supplier.AuthorizationStatement = AuthorizationStatement.Value;
            _supplier.ContractNumber = ContractNumber.Text;
            _supplier.ContractStatement = ContractStatement.Value;
            _supplier.Status = status;
            _supplier.Version = "1";
            _supplier.CreateDate = DateTime.Now;
            _supplier.Password = Password.Text;
            _supplier.DiscountPercent = ConvertUtility.ToDouble(DiscountPercent.Text);
            _supplier.PricePercent = ConvertUtility.ToDouble(PricePercent.Text);
            _supplier.PointPercent = ConvertUtility.ToDouble(PointPercent.Text);
            _supplier.Username = this.Username.Text.Trim();
            _supplier.Introduction = Introduction.Value;

            _supplier.Logo = Logo.Value;
            _supplier.LogoHome = LogoHome.Value;
            _supplier.Banner_Uudai = BannerUudaidacbiet.Value;

            _supplier.Website = WebsiteAdd.Text;
            _supplier.Gmap = fckGoogleMap.Value;

            _supplier.DiscountPercent = ConvertUtility.ToDouble(DiscountPercent.Text);
            _supplier.PricePercent = ConvertUtility.ToDouble(PricePercent.Text);
            _supplier.PointPercent = ConvertUtility.ToDouble(PointPercent.Text);

            //_supplier.DiamondDiscountPercent = ConvertUtility.ToDouble(DiamondDiscountPercent.Text);
            //_supplier.DiamondPricePercent = ConvertUtility.ToDouble(DiamondPricePercent.Text);
            //_supplier.DiamondPointPercent = ConvertUtility.ToDouble(DiamondPointPercent.Text);

            //_supplier.PlatinumDiscountPercent = ConvertUtility.ToDouble(PlatinumDiscountPercent.Text);
            //_supplier.PlatinumPricePercent = ConvertUtility.ToDouble(PlatinumPricePercent.Text);
            //_supplier.PlatinumPointPercent = ConvertUtility.ToDouble(PlatinumPointPercent.Text);
            _supplier.Tags = txtTags.Text;

            //Lựa cọn Cate cho Đối tác
            _supplier.SupplierCateID = ConvertUtility.ToInt32(CategoryID.SelectedValue);

            //Lựa chọn ẩn/hiển trang chủ
            _supplier.SupplierTransparency = SupplierTransparency.Checked;
            _supplier.SupplierParentID = Convert.ToInt64(SupplierParentID.SelectedValue);

            dao.Add(_supplier);
            nSupplierID = _supplier.SupplierID;

            //Thêm tags - các tag cách nhau bởi dấu ,
            if (!string.IsNullOrEmpty(_supplier.Tags))
            {
                var addTag = new TagDao().AddSupplierTag(nSupplierID, _supplier.Tags);
            }
        }

        protected void addSupplierTemp()
        {
            var dao = new SupplierTempDao();

            var _supplier = new LinkCard.Model.SupplierTemp();

            _supplier.Name = Name.Text;
            _supplier.Director = Director.Text;
            _supplier.Address = Address.Text;
            _supplier.Phone = Phone.Text;
            _supplier.Fax = Fax.Text;
            _supplier.Email = Email.Text;
            _supplier.TaxCode = TaxCode.Text;
            _supplier.ContactPersonName = ContactPersonName.Text;
            _supplier.ContactPersonMail = ContactPersonMail.Text;
            _supplier.Business = Business.Text;
            _supplier.AuthorizationStatement = AuthorizationStatement.Value;
            _supplier.ContractNumber = ContractNumber.Text;
            _supplier.ContractStatement = ContractStatement.Value;
            _supplier.Status = 1;
            _supplier.Version = "1";
            _supplier.CreateDate = DateTime.Now;
            _supplier.SupplierID = nSupplierID;
            _supplier.DateLog = DateTime.Now;
            _supplier.UserID = getUserID();
            _supplier.Password = Password.Text;
            _supplier.DiscountPercent = ConvertUtility.ToDouble(DiscountPercent.Text);
            _supplier.PricePercent = ConvertUtility.ToDouble(PricePercent.Text);
            _supplier.PointPercent = ConvertUtility.ToDouble(PointPercent.Text);

            //_supplier.DiamondDiscountPercent = ConvertUtility.ToDouble(DiamondDiscountPercent.Text);
            //_supplier.DiamondPricePercent = ConvertUtility.ToDouble(DiamondPricePercent.Text);
            //_supplier.DiamondPointPercent = ConvertUtility.ToDouble(DiamondPointPercent.Text);

            //_supplier.PlatinumDiscountPercent = ConvertUtility.ToDouble(PlatinumDiscountPercent.Text);
            //_supplier.PlatinumPricePercent = ConvertUtility.ToDouble(PlatinumPricePercent.Text);
            //_supplier.PlatinumPointPercent = ConvertUtility.ToDouble(PlatinumPointPercent.Text);

            _supplier.Username = this.Username.Text.Trim();
            _supplier.Introduction = Introduction.Value;
            _supplier.Logo = Logo.Value;
            _supplier.LogoHome = LogoHome.Value;
            _supplier.Banner_Uudai = BannerUudaidacbiet.Value;

            _supplier.Website = WebsiteAdd.Text;
            _supplier.Gmap = fckGoogleMap.Value;
            _supplier.Tags = txtTags.Text;
            
            dao.Add(_supplier);

        }

        protected Int64 getUserID()
        {

            DataTable dt = LinkCard.Utils.DataHelper.SelectTable("SELECT UserID FROM [User] WHERE Username ='" + Session["USER"].ToString() + "'", false);
            if (dt.Rows.Count > 0)
            {
                return ConvertUtility.ToInt64(dt.Rows[0]["UserID"]);
            }
            else
            {
                return 0;
            }
        }
    }
}
